package com.deo.freemark.util;

import java.io.StringWriter;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkUtil {
	private static Configuration CFG = null;

	private static synchronized void init() {
		if (CFG == null) {
			CFG = new Configuration();
			CFG.setClassForTemplateLoading(FreemarkUtil.class, "/");
			// cfg.setDirectoryForTemplateLoading(new File("templates"));
			// cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
			CFG.setDefaultEncoding("UTF-8");
		}
	}

	public static String process(Object parm, String fileName) {
		try {
			init();
			Template temp = CFG.getTemplate(fileName);
			StringWriter sw = new StringWriter();
			temp.process(null, sw);
			return sw.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

}
